<?php
namespace app\index\model;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 短信日志类
 */
class LogSms extends Base{

	/**
	 * 写入并发送短讯记录
	 */
	public function sendSMS($tpl_id,$sms_phone,$sms_code){
		$ip = request()->ip();
		
		//检测IP是否超过发短信次数
		$count = $this->where(["sms_ip"=>$ip])->whereTime('create_time', 'd')->count();
		if($count > 50){
			return HTRReturn("请勿频繁发送短信验证!");
		}
		$sms = $this->get($sms_phone);
		if(!empty($sms)){
			if($sms->create_time !='' && ((time()-$sms->create_time)<60)){
				return HTRReturn("请勿频繁发送短信验证!");
			}
		}
		$code = HTRSendSMS($tpl_id,$sms_phone,'#code#='.$sms_code);
		if(intval($code)>0){
			$data = array();
			$data['sms_code'] = $sms_code;
			$data['sms_ip'] = $ip;
			$data['create_time'] = time();
			if($sms){
				$this->save($data,['sms_phone'=>$sms_phone]);
			}else{
				$data['sms_phone'] = $sms_phone;
				$this->save($data);
			}
			return HTRReturn("短信发送成功!",1);
		}else{
			return HTRReturn("短信发送失败!");
		}
	}

	/**
	 * 验证短信
	 */
	public function checkSMS($sms_phone,$sms_code){
		$create_time = $this->where(["sms_phone"=>$sms_phone,"sms_code"=>$sms_code])->value("create_time");
		if(time() < $create_time+600){
			return HTRReturn("短信验证码可用!",1);
		}else{
			return HTRReturn("短信验证码错误!");
		}
	}
}
